﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using LayerDTO;

namespace LayerDAO
{
    public class DonViDAO
    {
        public List<DonViDTO> LayDanhSachDonVi(string connect)
        {
            DataTable dt = new DataTable();
            DataProvider _provider = new DataProvider(connect);
            if (_provider.OpenConnect())
                dt = _provider.SqlExcuteQuery("LayDanhSachDonVi", null);
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            List<DonViDTO> list = new List<DonViDTO>();
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DonViDTO donVi = new DonViDTO();
                    donVi.MaDV = dt.Rows[i]["MDV"].ToString();
                    donVi.TenDV = dt.Rows[i]["TenDV"].ToString();
                    donVi.DaXoa = (bool)dt.Rows[i]["DaXoa"];
                    list.Add(donVi);
                }
            }
            return list;
        }
        public DonViDTO LayDanhSachDonViTheoMaDonVi(string connect,string ma)
        {
            DataTable dt = new DataTable();
            DataProvider _provider = new DataProvider(connect);
            if (_provider.OpenConnect())
                dt = _provider.SqlExcuteQuery("LayDonViTheoMaDonVi", new SqlParameter("@ma", ma));
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            DonViDTO donVi = null;
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    donVi = new DonViDTO();
                    donVi.MaDV = dt.Rows[i]["MDV"].ToString();
                    donVi.TenDV = dt.Rows[i]["TenDV"].ToString();
                    donVi.DaXoa = (bool)dt.Rows[i]["DaXoa"];
                }
            }
            return donVi;
        }
        public string ThemDonVi(DonViDTO donVi, string connect)
        {

            DataProvider _provider = new DataProvider(connect);
            SqlParameter par = new SqlParameter("@return", SqlDbType.Int);
            par.Direction = ParameterDirection.ReturnValue;
            if (_provider.OpenConnect())
            {
                _provider.SqlExcuteScalar("ThemDonVi", new SqlParameter("@tenDV", donVi.TenDV), par);

            }
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            return par.Value.ToString();
        }

        public string SuaDonVi(DonViDTO donVi, string connect)
        {

            DataProvider _provider = new DataProvider(connect);
            SqlParameter par = new SqlParameter("@return", SqlDbType.Int);
            par.Direction = ParameterDirection.ReturnValue;
            if (_provider.OpenConnect())
            {
                _provider.SqlExcuteScalar("SuaDonVi", new SqlParameter("@maDV", donVi.MaDV),
                                                              new SqlParameter("@tenDV", donVi.TenDV),

                                                              new SqlParameter("@daXoa", donVi.DaXoa), par);

            }
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            return par.Value.ToString();
        }
        public string XoaDonVi(DonViDTO donVi, string connect)
        {

            DataProvider _provider = new DataProvider(connect);
            SqlParameter par = new SqlParameter("@return", SqlDbType.Int);
            par.Direction = ParameterDirection.ReturnValue;
            if (_provider.OpenConnect())
            {
                _provider.SqlExcuteScalar("XoaDonVi", new SqlParameter("@maDV", donVi.MaDV), par);


            }
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            return par.Value.ToString();
        }
    }
}